package cn.demoncat.util.exception;

import java.util.function.Supplier;

/**
 * 自定义运行时异常根类
 *
 * @author 延晓磊
 *
 * @since 2017年3月26日
 */
public abstract class BaseRuntimeException extends RuntimeException{

	// 错误码
	private String code;

	public BaseRuntimeException() {
		super();
	}
	
	public BaseRuntimeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
	}

	public BaseRuntimeException(String message, Throwable cause) {
		super(message, cause);
	}

	public BaseRuntimeException(String message) {
		super(message);
	}

	public BaseRuntimeException(Throwable cause) {
		super(cause);
	}

	public BaseRuntimeException(String message, String code, Throwable cause) {
		super(message, cause);
		this.code = code;
	}

	public BaseRuntimeException(String message, String code) {
		super(message);
		this.code = code;
	}

	/**
	 * 错误码
	 *
	 * @return
	 */
	public String getCode() {
		return code;
	}

	/**
	 * 错误码
	 *
	 * @param code
	 * @return
	 */
	public BaseRuntimeException setCode(String code) {
		this.code = code;
		return this;
	}

	/**
	 * 获取结果
	 *
	 * @param supplier
	 * @param <T>
	 * @return	抛异常时返回 null
	 *
	 * @author 延晓磊
	 * @since 2022年08月01日
	 */
	public static <T>T get(Supplier<T> supplier){
		try {
			return supplier.get();
		}catch (BaseRuntimeException e){
			return null;
		}
	}

}
